//
//  AddMemberNotesView.m
//  location
//
//  Created by Dongxiang Xu on 2025/5/8.
//

#import "AddMemberNotesView.h"

@interface AddMemberNotesView ()<UITextFieldDelegate>

@property (nonatomic,strong) UIView *infoView;
@property (nonatomic,strong) NSDictionary *userInfo;

@end

@implementation AddMemberNotesView

- (instancetype)initWithFrame:(CGRect)frame
                     UserInfo:(NSDictionary *)userInfo
{
    self = [super initWithFrame:frame];
    if (self) {
        _userInfo = userInfo;
        [self initViewWithUserInfo:userInfo];
    }
    return self;
}

- (void)initViewWithUserInfo:(NSDictionary *)userInfo
{
    _infoView = [[UIView alloc] initWithFrame:CGRectMake(15, SCREEN_HEIGHT, SCREEN_WIDTH-30, 280)];
    _infoView.layer.masksToBounds = YES;
    [_infoView.layer setCornerRadius:26];
    _infoView.backgroundColor = RealWhiteColor;
    [self addSubview:_infoView];
    
    UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(_infoView.width-46, 16, 30, 30)];
    [close setImage:[UIImage imageNamed:@"map_close"] forState:UIControlStateNormal];
    [_infoView addSubview:close];
    [[close rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 animations:^{
        self.infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(32, 46, _infoView.width-64, 34)];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"修改备注";
    title.textColor = BlackColor;
    title.font = MEDIUM_FONT(20);
    [_infoView addSubview:title];
    
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(17, CGRectGetMaxY(title.frame)+24, _infoView.width-34, 52)];
    inputView.layer.masksToBounds = YES;
    [inputView.layer setCornerRadius:16];
    inputView.backgroundColor = LightBGColor;
    [_infoView addSubview:inputView];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(16, 0, inputView.width-32, inputView.height)];
    textField.font = DEFAULT_FONT(14);
    textField.textColor = RealBlackColor;
    textField.placeholder = @"添加备注名";
    textField.text = userInfo[@"remark"];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.returnKeyType = UIReturnKeyDone;
    textField.delegate = self;
    textField.enablesReturnKeyAutomatically = YES;
    [textField becomeFirstResponder];
    [textField addTarget:self action:@selector(textChangeEvent:) forControlEvents:UIControlEventEditingChanged];
    [inputView addSubview:textField];
    
    NSArray *array = @[@"取消",@"确定"];
    for (int i = 0; i < array.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(32+i*((_infoView.width-80)/2+16), inputView.maxY+26, (_infoView.width-80)/2, 52)];
        button.layer.masksToBounds = YES;
        [button.layer setCornerRadius:26];
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button.titleLabel setFont:DEFAULT_FONT(17)];
        if (i==0) {
            button.backgroundColor = WhiteColor;
            [button setTitleColor:RealBlackColor forState:UIControlStateNormal];
        }else{
            button.backgroundColor = NormalMainColor;
            [button setTitleColor:RealWhiteColor forState:UIControlStateNormal];
        }
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (i==0) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.infoView.y = SCREEN_HEIGHT;
                    self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
                } completion:^(BOOL finished) {
                    [self removeFromSuperview];
                }];
            }else if (i==1){
                if (textField.text.length>0) {
                    [self textFieldShouldReturn:textField];
                }else{
                    [[HCToast shareInstance] showToast:@"请输入备注"];
                }
            }
        }];
        [_infoView addSubview:button];
    }
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self insertSubview:bgView atIndex:0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        [UIView animateWithDuration:0.3 animations:^{
            self.infoView.y = SCREEN_HEIGHT;
            self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
    [bgView addGestureRecognizer:tap];
            
    [UIView animateWithDuration:0.3 animations:^{
        self.infoView.y = 212;
        self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0.4];
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:textField.text forKey:@"remark"];
    [param setValue:_userInfo[@"userId"] forKey:@"userId"];
    [param setValue:_userInfo[@"id"] forKey:@"groupId"];
    CryptoHTTPKit *manager = [[CryptoHTTPKit alloc] init];
    [manager encryptedPUT:@"/system/group/edituserremark"
    parameters:param
    success:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue]==200) {
            [UIView animateWithDuration:0.3 animations:^{
                self.infoView.y = SCREEN_HEIGHT;
                self.backgroundColor = [RealBlackColor colorWithAlphaComponent:0];
            } completion:^(BOOL finished) {
                [XDXShareInstance shareInstance].ReloadGroupListFlag = YES;
                [self removeFromSuperview];
                if (self.AddMemberNotesBlock) {
                    self.AddMemberNotesBlock(textField.text);
                }
            }];
        }
        [[HCToast shareInstance] showToast:responseObject[@"msg"]];
    }
    failure:^(NSError *aError) {
        
    }];
        
    return YES;
}

- (void)textChangeEvent:(UITextField *)textField
{
    if (textField.text.length>=20) {
        textField.text = [textField.text substringToIndex:20];
    }
}

@end
